package com.mingzhi.order.controller;

import com.mingzhi.order.bean.Order;
import com.mingzhi.order.properties.OrderProperties;
import com.mingzhi.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

//@RefreshScope
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

   /* @Value("${order.timeout}")
    private String orderTimeout;
    @Value("${order.auto-confirm}")
    private String orderAutoConfirm;*/

    @Autowired
    private OrderProperties  orderProperties;



    @GetMapping ("/config")
    public String config(){
        return "order.timeout=" + orderProperties.getTimeout() +
                ",order.autoConfirm=" + orderProperties.getAutoConfirm() +
                ",order.dbUrl=" + orderProperties.getDbUrl();
    }

    //创建 订单
    @RequestMapping("/create")
    public Order createOrder(@RequestParam("userId") Long userId,
                             @RequestParam("productId") Long productId) {
        Order order = orderService.createOrder(userId, productId);
        return order;
    }

    //秒杀订单
    @RequestMapping("/seckill")
    public Order seckill(@RequestParam("userId") Long userId,
                             @RequestParam("productId") Long productId) {
        Order order = orderService.createOrder(userId, productId);
        order.setId(Long.MAX_VALUE);
        order.setNickName("秒杀用户");
        order.setAddress("秒杀地址");
        return order;
    }

    @Value("${spring.application.name}")
    private  String appName;
    @Value("${server.port}")
    private  String serverPort;
    @GetMapping(value = "/nacos/service/{str}")
    public  String getNacosService(@PathVariable("str") String str) {
        return  "<h2>服务访问成功！</h2>服务名称："+appName+"， 端口号："
                + serverPort +"<br /> 传入的参数：" + str;
    }
}
